home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / Libraries / VideoToolbox 97.08.16 / VideoToolboxSources / WaitSeconds.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-07-09  |  2.4 KB  |  75 lines  |  [TEXT/CWIE]

  1. /*
  2. WaitSeconds.c
  3.  
  4. Accurate delay that yields time to other processes. Exits if user types command-period.
  5.  
  6. Giving up time to the Mac OS is good, but dicey, since it might take more than the
  7. number of ticks that we offered. 
  8.  
  9. Checking whether the user hit Command-Period is good, but dicey, for the same reason.
  10. That check calls EventAvail, which gives up time to the Mac OS.
  11.  
  12. Our solution is to give up time (and check for cmd-.) while we have lots of time
  13. (more than 1/2 s), and to just run a tight loop, checking Seconds(), during the
  14. final countdown. This gives us the best of both worlds. Accurate timing, yet
  15. allow other processes to run during long waits, and allow user to cancel long waits.
  16.  
  17. REFERENCE:
  18. TechNote 1083 Weak-linking to a Code Fragment Manager-based shared library
  19. http://devworld.apple.com/dev/technotes/tn/tn1083.html
  20.  
  21. HISTORY:
  22. 2/3/97    dgp    wrote it, for use in David Brainard's Psychophysics Toolbox for MATLAB.
  23. 3/10/97    dgp use the new CommandPeriod.c, which doesn't discard keyDown events.
  24.             Moved WaitNextEventOrQuit() to CommandPeriod.c
  25. 3/15/97    dgp Call neither WNE nor CommandPeriod during final 1/2 s.
  26. 4/15/97    dgp Don't call WNE if priority is raised, because there won't be any ticks.
  27. 4/18/97    dgp Return immediately if timer is nonfunctional.
  28. 7/9/97    dgp    Simplified the test for availability of UpTime, based on TN1083.
  29. */
  30.  
  31. #include "VideoToolbox.h" 
  32. #if GENERATINGCFM
  33.     #ifndef __CODEFRAGMENTS__
  34.         #include <CodeFragments.h>
  35.     #endif
  36. #endif
  37. typedef UnsignedWide AbsoluteTime;    // Types.h
  38. extern AbsoluteTime UpTime(void);    // DriverServices.h
  39.  
  40. void WaitSeconds(double secs)
  41. {
  42.     double finishSecs,ticks;    // ticks is double to avoid overflow problems
  43.     EventRecord event;
  44.     Boolean wne,haveUpTime;
  45.     
  46.     #if GENERATINGCFM && GENERATINGPOWERPC
  47.         haveUpTime= (Ptr)UpTime != (Ptr)kUnresolvedCFragSymbolAddress;
  48.     #else
  49.         haveUpTime=0;
  50.     #endif
  51.     if(IsNan(secs) || secs<0)return;
  52.     if(GetPriority()>0 && !haveUpTime)return;
  53.     finishSecs=Seconds()+secs;
  54.     for(wne=0;;wne=!wne){
  55.         ticks=60.15*(finishSecs-Seconds());
  56.         if(ticks<=0.)break;
  57.         if(ticks>=30.)switch(wne && GetPriority()==0){    // don't fool around close to the end.
  58.         default:
  59.         case 1:
  60.             WaitNextEvent(0,&event,6,NULL);    // be a good citizen, share 1/10 s.
  61.             break;
  62.         case 0:
  63.             if(CommandPeriod()){    // calls EventAvail, which gives up time
  64.                 #if MATLAB
  65.                     PrintfExit("User typed cmd-. EXITING.");
  66.                 #else
  67.                     PrintfExit("\nUser typed cmd-. EXITING.\n");
  68.                 #endif
  69.             }
  70.             break;
  71.         }
  72.     }
  73. }
  74.  
  75.